package com.attilax.ioc;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

//import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.engine.spi.SessionFactoryImplementor;
//import org.hibernate.jdbc.Work;
//import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
//import org.springframework.orm.hibernate4.SessionFactoryUtils;

import java.util.Properties;

//import org.hibernate.Session;
//import org.hibernate.SessionFactory;

import javax.sql.DataSource;

import com.attilax.Closure;

import com.attilax.db.DBX;
import com.attilax.db.DbServiceV4qb9;
import com.attilax.hre.UrlDslParser;
import com.attilax.hre.UrlDslParserV3;
import com.attilax.io.pathx;
import com.attilax.json.AtiJson;
import com.attilax.lang.Closure2;
import com.attilax.lang.Trigger;
import com.attilax.net.HttpRequest;
import com.attilax.net.requestImp;
import com.attilax.net.websitex;
import com.attilax.order.OrderService;
import com.attilax.order.OrderService4jobus;
import com.attilax.persistence.DBCfg;
import com.attilax.persistence.DbutilX;
//import com.attilax.db.Hb4JdbcX;
//import com.attilax.lang.Closure2;
import com.attilax.persistence.Hbx;
import com.attilax.persistence.HbxX;
import com.attilax.persistence.PX;
import com.attilax.sms.Sms1xinxi;
import com.attilax.sms.SmsService;
import com.attilax.spri.SpringUtil;
import com.attilax.sql.ConnectionImp;
import com.attilax.user.UserService;
import com.attilax.user.UserSrvImpDistribVer;
import com.attilax.util.HibernateSessionFactory;
import com.attilax.util.PropX;
import com.attilax.web.UrlDslParserIntr;
// import com.csmy.my.center.service.UserInfoService;
//import com.focustar.entity.TMbWeixinuser;
//import com.focustar.util.BaseImpl;
//import com.focusx.dao.BranchManagerDao;
//import com.focusx.dao.impl.BranchManagerDaoImpl;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.name.Names;

/**
 * com.attilax.ioc.Ioc4agent aaaCfg.IocX4jobus
 * 
 * @author Administrator
 *
 */
public class Ioc4Test {
	static String cfgname = "other_cfgfile";
	static Injector ati;
	// public static void main(String[] args) {
	// System.out.println(getCfgVal("hre_web_url"));
	//
	// }
	public static String dbcfg_file;

	// pathx
	// .webAppPath()
	// + "/WEB-INF/classes/jdbc.properties";
	public static String getCfgVal(String string) {
		String apptype = System.getProperty("apptype");
		String cfgpath = pathx.webAppPath() + "/" + apptype + ".cfg.txt";
		PropX px = new PropX(cfgpath);
		return px.getProperty(string);
	}

	public static Map<String, Object> map = new HashMap();

	public static Map ini() {
		// map.put("getUserinfoFrmWechatHandler", new
		// Closure<TMbWeixinuser,Object>(){
		//
		// @Override
		// public Object execute(TMbWeixinuser arg0)
		// throws Exception {
		// ExtWebsiteX x=new ExtWebsiteX();
		// x.getUserinfoFrmWechatHandler(arg0);
		// return null;
		// }});
		try {
			map.put("sprx", new Closure() {

				@Override
				public Object execute(Object arg0) throws Exception {
					String p = pathx.classPath() + "/";
					SpringUtil.locations = new String[] { p + "applicationContext.xml" };
					return new SpringUtil();
				}
			}.execute(null));
		} catch (Exception e) {

			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return map;

	}

	public static void main(String[] args) {

		System.setProperty("other_cfgfile", "cfg_game2.properties");
		DbServiceV4qb9 dbc = Ioc4Test.getBean(DbServiceV4qb9.class);
		;
		String sql = "select 'ok' as col";
		// sql="insert wxb_news(news_title,news_content)values('t2','c2');"
		sql = "select count(*) as numx from wxb_news;select * from wxb_news limit 2;";
		sql="insert wxb_news(news_title,news_content,news_state,create_time)values('t2','c2',0,'2017-1-1');"+sql;
	//	sql="call multi";
		//Object r = dbc.executeQuery(sql);
	 Object r = dbc.executeQuery_multisql(sql);
		System.out.println(AtiJson.toJson(r));
		System.out.println("aa");
	}

	/**
	 * @author attilax 老哇的爪子
	 * @since o92 j_48_40
	 * 
	 * @param class1
	 * @return
	 */
	public static <t> t getBean(Class<t> class1) {
		// attilax 老哇的爪子 j_48_40 o92
		// try {

		if (System.getProperty(cfgname) == null)
			throw new RuntimeException(" System.getProperty( " + cfgname + " )==null");
		dbcfg_file = pathx.webAppPath() + "/" + System.getProperty(cfgname);
		if (ati == null) // ini singleon patter..
		{
			Module module = new Module() {

				@Override
				public void configure(Binder bd) {

					AnnotatedBindingBuilder<DBCfg> bind = bd.bind(DBCfg.class);
					DBCfg dbcfg1 = getDbcfg().execute(null);
					bind.toInstance(dbcfg1);

					/**
					 * // bd.bind(DBX.class).to(DbutilX.class);
					 * bd.bind(Connection.class).to(ConnectionImp.class);
					 * bd.bind(UrlDslParser.class).to(UrlDslParserV3.class);
					 * bd.bind
					 * (UserService.class).to(UserSrvImpDistribVer.class);
					 * bd.bind(SmsService.class).to(Sms1xinxi.class);
					 * bd.bind(OrderService.class).to(OrderService4jobus.class);
					 * //
					 * bd.bind(DbServiceV4qb9.class).to(DbServiceV4qb9.class);
					 * 
					 * bd.bindConstant().annotatedWith(Names.named("thql"))
					 * .to(" from TUserUsers ");
					 **/
				}
			};
			ati = Guice.createInjector(module);
		}

		return ati.getInstance(class1);

	}

	public static Closure2<Object, DBCfg> getDbcfg() {
		Closure2<Object, DBCfg> closure2 = new Closure2<Object, DBCfg>() {

			@Override
			public DBCfg execute(Object arg0) {
				DBCfg cfg = new DBCfg();
				try {
					// propertyReader pro=new Properties();

					FileInputStream fis = new FileInputStream(new File(dbcfg_file));
					Properties properties = new Properties();
					properties.load(fis);
					// pro.
					String url = properties.getProperty("jdbc.url");
					String driver = properties.getProperty("jdbc.driverClassName");

					String uname = properties.getProperty("jdbc.username");
					String pwd = properties.getProperty("jdbc.password");
					//
					// configuration.configure(propertyFile);
					// String url =
					// configuration.getProperty("connection.url");
					// String driver = configuration
					// .getProperty("connection.driver_class");
					//
					// String uname =
					// configuration.getProperty("connection.username");
					// String pwd =
					// configuration.getProperty("connection.password");
					// // String url =
					// "jdbc:mysql://@host/@db?zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8";

					cfg.setUrl(url);
					cfg.setUser(uname);
					cfg.setPassword(pwd);
					cfg.setDriver(driver);
					return cfg;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					throw new RuntimeException("cant ini dbcfg", e);
				}

			}
		};
		return closure2;
	}

}
